package org.fuxian.controller;

import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.fuxian.dao.BusMapper;
import org.fuxian.dao.BusOrderMapper;
import org.fuxian.model.Bus;
import org.fuxian.model.BusOrder;
import org.fuxian.model.vo.Location;
import org.fuxian.util.BaiduMapUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/bus")
public class BusController {

    @Autowired
    BusMapper busMapper;

    @Autowired
    BusOrderMapper busOrderMapper;

    @RequestMapping("/list")
    @ResponseBody
    public Map<String, Object> list(@RequestParam(value = "page", defaultValue = "1") Integer page) {
        Map<String, Object> returnMap = new HashMap<>();
        returnMap.put("code", "200");

        PageHelper.startPage(page, 5);
        List<Bus> busList = busMapper.selectAll();
        PageInfo<Bus> pageInfo = new PageInfo<>(busList, 5);
        returnMap.put("pageInfo", pageInfo);
        return returnMap;
    }

    @RequestMapping("/getOneById")
    @ResponseBody
    public Map<String, Object> getOne(@RequestParam(value = "id") Integer id) {
        Map<String, Object> map = new HashMap<>();
        Bus bus = busMapper.selectByPrimaryKey(id);

        if (bus != null) {
            map.put("code", "200");
            map.put("bus", bus);
        } else {
            map.put("code", "500");
        }
        return map;
    }


    @RequestMapping("/update")
    @ResponseBody
    public Map<String, Object> update(@RequestBody(required = true) Bus bus) {
        Map<String, Object> map = new HashMap<>();
        int count = busMapper.updateByPrimaryKey(bus);
        if (count > 0) {
            map.put("code", "200");
        } else {
            map.put("code", "500");
        }
        return map;
    }

    @RequestMapping("/new")
    @ResponseBody
    public Map<String, Object> insert(@RequestBody(required = true) Bus bus) {
        Map<String, Object> map = new HashMap<>();
        String startPoint = bus.getStartPoint();
        String endPoint = bus.getEndPoint();
        //根据中文地址获取经纬度
        Location startLocation = new Location(bus.getStartLng(), bus.getStartLat());
        Location endLocation = new Location(bus.getEndLng(), bus.getEndLat());
        bus.setStartLocation(startLocation);
        bus.setEndLocation(endLocation);

        //存储路径字符串
        String pathJson = BaiduMapUtil.getPathJsonStrByLocation(startLocation, endLocation);
        bus.setPath(pathJson);


        int count = busMapper.insert(bus);
        if (count > 0) {
            map.put("code", "200");
        } else {
            map.put("code", "500");
        }
        return map;
    }


    @RequestMapping("/del")
    @ResponseBody
    public Map<String, Object> delete(@RequestParam(value = "id") Integer id) {
        Map<String, Object> map = new HashMap<>();
        int count = busMapper.deleteByPrimaryKey(id);
        if (count > 0) {
            map.put("code", "200");
        } else {
            map.put("code", "500");
        }
        return map;
    }

    @RequestMapping("/newOrder")
    @ResponseBody
    public Map<String, Object> newOrder(@RequestBody(required = false) Map orderMap) {
        Map<String, Object> map = new HashMap<>();
        JSONObject obj = new JSONObject(JSONUtil.toJsonStr(orderMap));
        JSONObject location = obj.getJSONObject("location");
        BusOrder busOrder = new BusOrder();
        busOrder.setBusId(obj.getInt("busId"));
        busOrder.setDate(obj.getStr("date"));
        busOrder.setStartPoint(location.getStr("startPoint"));
        busOrder.setEndPoint(location.getStr("endPoint"));
        int count = busOrderMapper.insert(busOrder);
        if (count > 0) {
            map.put("code", "200");
            map.put("msg", location.getStr("startPoint"));
        } else {
            map.put("code", "500");
        }
        return map;
    }


    @RequestMapping("/busOrderList")
    @ResponseBody
    public Map<String, Object> busOrderList(@RequestParam(value = "page", defaultValue = "1") Integer page) {
        Map<String, Object> returnMap = new HashMap<>();
        returnMap.put("code", "200");

        PageHelper.startPage(page, 5);
        List<BusOrder> busOrderList = busOrderMapper.selectAll();
        PageInfo<BusOrder> pageInfo = new PageInfo<>(busOrderList, 5);
        returnMap.put("pageInfo", pageInfo);
        return returnMap;
    }

    @RequestMapping("/busOrder/list")
    @ResponseBody
    public Map<String, Object> busOrderListByUserId(@RequestParam(value = "page", defaultValue = "1") Integer page,@RequestParam(value = "id") Integer id) {
        Map<String, Object> returnMap = new HashMap<>();
        returnMap.put("code", "200");

        PageHelper.startPage(page, 5);
        List<BusOrder> busOrderList = busOrderMapper.selectAllByUserId(id);
        PageInfo<BusOrder> pageInfo = new PageInfo<>(busOrderList, 5);
        returnMap.put("pageInfo", pageInfo);
        return returnMap;
    }

}
